﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BrsdServer
{
    public partial class dnslist : System.Web.UI.Page
    {
        dnsInfoBLL dnsInfo = new dnsInfoBLL();
        userInfoBLL userInfo = new userInfoBLL();
        DataTable dt = new DataTable();
        public static String userName = "";
        String pwd = "";
        int rowNum = 0;   //记录的条数，用来在序号栏目中显示
        //查看超链接字符串的组成。
        /*此处注意与前台页面的<%# lookUrl %>的冲突，当前台页面一出来的时候会调用这个lookUrl
         导致行的值错位。*/
        // public String lookUrl = "#";
        public int port = 0;  //组成一个端口号，来源于自定义的dns_port表。
        //是为了兼容恒亿的dns_info表结构。查表过程是对于取出来的dns_info中
        //的每一个MAC到dns_port表中查找相应的记录。如果有记录显示，没有
        //就设定相应的字段为空或其他字串“端口没有设定”

        private void GVDataBind()
        {

            //根据用户类型来确定显示数据
            if (userInfo.GetUserKind(userName, pwd) == 10)
            {
                dt = dnsInfo.LoadAllDataTable();
            }
            else
            {
                dt = dnsInfo.LoadAllDataTable(userName);
            }
            gvDnsInfo.DataSource = dt.DefaultView;
            gvDnsInfo.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if ((String)Session["nameSession"] == "" || (String)Session["pwdSession"] == ""
                || Session["nameSession"] == null || Session["pwdSession"] == null)
            {
                Response.Redirect("index.aspx");
                return;
            }
            userName = (String)Session["nameSession"];
            pwd = (String)Session["pwdSession"];
            GVDataBind();
        }

        protected void gvDnsInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                TextBox txtNewPageIndex = null;
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {

                newPageIndex = e.NewPageIndex;
            }

            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            //得到新的值
            theGrid.PageIndex = newPageIndex;
            //重新绑定         
            GVDataBind();
        }

        //绑定每一行时发生的事件
        protected void gvDnsInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int i;
            //执行循环，保证每条数据都可以更新
            for (i = -1; i < gvDnsInfo.Rows.Count; i++)
            {
                //首先判断是否是数据行
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
            }
            if (e.Row.RowIndex != -1)
            {
                rowNum = e.Row.RowIndex + 1;
                e.Row.Cells[0].Text = rowNum.ToString();
                //域名的显示规则：当dns_name字段为空时则显示为：mac.ptdvr.com
                //否则显示为：dns_name.ptdvr.com
                String macStr = e.Row.Cells[4].Text;
                DnsAndState dnsState = dnsInfo.GetDnsNameAndStateByMac(macStr);
                if (dnsState.dns_name == "")
                {
                    e.Row.Cells[1].Text = macStr + ".ptdvr.com";
                }
                else
                {
                    e.Row.Cells[1].Text = dnsState.dns_name + ".ptdvr.com";
                }
                string dvr_Addr = dnsInfo.GetDvrAddress(macStr);
                if (dvr_Addr.Length > 10)
                {
                    e.Row.Cells[6].Text = dvr_Addr.Substring(0, 10);
                }
                else
                {
                    e.Row.Cells[6].Text = dvr_Addr;
                }


                //判断DVR的连接状态。一种方式为：当数据库中的字段为0时显示“离线”，
                //为1时显示“在线”。还有一种方式，通过DVR发送端来响应。如果长时间
                //心跳超时则设定为“离线”
                if (dnsState.dvr_state)
                {
                    e.Row.Cells[9].Text = "在线";
                }
                else
                {
                    e.Row.Cells[9].Text = "离线";
                }
                // http://2222.ptdvr.com:8080 格式
                if (port != 0)
                {
                    e.Row.Cells[3].Text = port.ToString();
                }

            }
        }

        protected void gvDnsInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (userName != "Admin")
            {
                string strScript = "<script>alert('您不是管理员用户没有权限删除信息!')</script>";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", strScript);
                return;
            }
            else
            {
                string mac = gvDnsInfo.Rows[e.RowIndex].Cells[4].Text;
                dnsInfo.Delete(mac);
                GVDataBind();
                //gvDnsInfo.DataBind();
            }
        }        


    }
}
